package main;

import config.ConfigMaker;
import define.Common;
import define.Config;
import define.ErrorDefine;
import excelBase.ExcelData;

public class Args
{
	private String[] m_args;
	
	public Args(String[] args)
	{
		m_args = args;
	}
	
	public void execute() throws ErrorException
	{
		int start = find(Config.ARG_INPUT, 0);
		if(start == Config.NO_FIND)
		{
			throw new ErrorException(ErrorDefine.E_ARGS_NO_INPUT_ERROR, Config.NO_POSITION);
		}
		new ConfigMaker(m_args, 0, start - 1, Config.NO_POSITION);
		int end = find(Config.ARG_SCRIPT, start + 1);
		if(end == Config.NO_FIND)
		{
			end = m_args.length;
		}
		else if(end == m_args.length - 1)
		{
			throw new ErrorException(ErrorDefine.E_ARGS_NO_SCRIPT_ERROR, Config.NO_POSITION);
		}
		if(end <= start)
		{
			throw new ErrorException(ErrorDefine.E_NO_INPUT_FILE_ERROR, Config.NO_POSITION);
		}
		if(end < m_args.length - 1)
		{
			Common.m_scriptSheet = m_args[end + 1];
		}
		for(++start; start < end; ++start)
		{
			ExcelData.readExcel(Common.m_path + m_args[start]);
		}
		ExcelData.complieScript();
		ExcelData.output();
		System.out.println("finish");
	}
	
	private int find(String dest, int start)
	{
		for(; start < m_args.length; ++start)
		{
			String temp = m_args[start].toLowerCase();
			if(temp.compareTo(dest) == 0)
			{
				return start;
			}
		}
		return Config.NO_FIND;
	}
}
